package com.egdk.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class UnitTest {

    private static String CHAR="\r\n";
    public static void main(String[] args) {

        String path="C:\\tmp\\result.json";
        try {
            String content= IOUtils.toString(new FileInputStream(path));

            
            JSONObject root=JSONObject.parseObject(content);

            root=root.getJSONObject("data");
            JSONObject studyObj=root.getJSONObject("Study");
            StringBuilder builder=new StringBuilder();
            builder.append("[TPatRecord]").append(CHAR);
            builder.append("Pat_PatID").append("=").append(studyObj.getString("PatientID")).append(CHAR);
            builder.append("Stu_StuDes").append("=").append(studyObj.getString("StudyDescription")).append(CHAR);
            builder.append("Pat_PatNam").append("=").append(studyObj.getString("PatientName")).append(CHAR);
            builder.append("Pat_PatSex").append("=").append(studyObj.getString("PatientSex")).append(CHAR);
            builder.append("Pat_PatBirDat").append("=").append(studyObj.getString("PatientBirthDate")).append(CHAR);
            builder.append("Stu_PatAge").append("=").append(studyObj.getString("PatientsAge")).append(CHAR);
            builder.append("Stu_StuDat").append("=").append(studyObj.getString("StudyDate")).append(CHAR);
            builder.append("Stu_StuInsUID").append("=").append(studyObj.getString("StudyInsta")).append(CHAR);
            builder.append("Stu_Priv").append("=").append("").append(CHAR);
            builder.append("Stu_PatSize").append("=").append(studyObj.getString("PatientSize")).append(CHAR);
            builder.append("Institution").append("=").append("").append(CHAR);
            builder.append("Manufacturer").append("=").append(studyObj.getString("Manufacturer")).append(CHAR);
            builder.append("ModelName").append("=").append(studyObj.getString("ModelName")).append(CHAR);
            builder.append("pat_uid").append("=").append(studyObj.getString("pat_uid")).append(CHAR);
            builder.append("pat_mid").append("=").append("").append(CHAR);
            builder.append("pat_name").append("=").append(studyObj.getString("pat_name")).append(CHAR);
            builder.append(CHAR);
            builder.append("iSerCount").append("=").append(studyObj.getString("iSerCount")).append(CHAR);
            builder.append("cBoxWindow").append("=").append(studyObj.getString("cBoxWindow")).append(CHAR);
            builder.append("Server").append("=").append("FUSION_SCREENCAP_A0662").append(CHAR);
            builder.append("ScreenForceTo").append("=").append(studyObj.getString("ScreenForceTo")).append(CHAR);
            builder.append("RegOpenMode").append("=").append("0").append(CHAR);
            builder.append(CHAR);

            JSONArray seriesArray=root.getJSONArray("Series");
            for (int i=0;i<seriesArray.size();i++){
                JSONObject seriesObj=seriesArray.getJSONObject(i);
                builder.append("["+i+"]").append(CHAR);
                builder.append("SerNum").append("=").append(seriesObj.getString("SeriesNumber")).append(CHAR);
                builder.append("Modality").append("=").append(seriesObj.getString("Modality")).append(CHAR);
                builder.append("SerDes").append("=").append(seriesObj.getString("SeriesDescription")).append(CHAR);
                builder.append("BodParExa").append("=").append(seriesObj.getString("BodyPartExamined")).append(CHAR);
                builder.append("SerInsUID").append("=").append(seriesObj.getString("SeriesInst")).append(CHAR);
                builder.append("StuParent").append("=").append(seriesObj.getString("StudyInsta")).append(CHAR);
                builder.append("Manufacturer").append("=").append(seriesObj.getString("Manufacturer")).append(CHAR);
                builder.append("ModelName").append("=").append(seriesObj.getString("ModelName")).append(CHAR);
                builder.append("RadionuclideTotalDose").append("=").append(seriesObj.getString("RadionuclideTotalDose")).append(CHAR);
                builder.append("RadionuclideStartTime").append("=").append(seriesObj.getString("RadionuclideStartTime")).append(CHAR);
                builder.append("NumSerRelIma").append("=").append(seriesObj.getString("NumSerRelIma")).append(CHAR);
                builder.append("iCount").append("=").append(seriesObj.getString("iCount")).append(CHAR);
                builder.append("iFrames").append("=").append(seriesObj.getString("NumFrames")).append(CHAR);
                JSONArray imageArray=seriesObj.getJSONArray("Images");
                for (int k=0;k<imageArray.size();k++){
                    JSONObject imageObj=imageArray.getJSONObject(i);
                    builder.append(k).append("=").append(imageObj.getString("ImageNumber")).append("@").append(imageObj.getString("ImagePositionPatient")).append("@").append("C:\\nanda\\data").append(imageObj.getString("ObjectFile")).append(CHAR);
                }
                builder.append(CHAR);
            }

            FileUtils.write(new File("C:\\tmp\\result.ini"),builder);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
